package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 权限管理分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = permissionService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 新增权限
     *
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result deleteById(Integer id) {
        try {
            permissionService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        }catch (RuntimeException e){
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }

    /**
     * 回显权限编辑窗口
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result findById(Integer id) {
        Permission permission = null;
        try {
            permission = permissionService.findById(id);
            return new Result(true, MessageConstant.DATA_ECHO_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DATA_ECHO_FAIL);
        }
    }

    /**
     * 编辑权限
     *
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result edit(@RequestBody Permission permission) {
        try {
            permissionService.edit(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    //      根据用户名查询用户的权限管理方面的权限
    @RequestMapping("/getPermissionByName")
//    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result getPermissionByName(String username) {
        try {
            List<String> list = permissionService.getPermissionByName(username);
            return new Result(true, "", list);
        } catch (Exception e) {
            return new Result(false, e.getMessage());
        }
    }
    //      根据用户名查询用户的菜单管理方面的权限
    @RequestMapping("/getMenuByName")
//    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result getMenuByName(String username) {
        try {
            List<String> list = permissionService.getMenuByName(username);
            return new Result(true, "", list);
        } catch (Exception e) {
            return new Result(false, e.getMessage());
        }
    }

    /**
     * 查询所有 权限信息
     */
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result findAll(){
        try {
            List<Permission> permissionList = permissionService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
}
